/**
 * Custom date/time picker.
 * 
 * @author Alican Ozgoren
 * @version 12/14/2012
 */

package edu.brockport.shuttleapp;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.ViewSwitcher;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.TimePicker.OnTimeChangedListener;

import edu.brockport.shuttleapp.R;

public class DateTimePicker extends RelativeLayout implements 
	View.OnClickListener, OnDateChangedListener, OnTimeChangedListener 
{
	private DatePicker		datePicker;
	private TextView		dayView;
	private TextView		dayView2;
	private TimePicker		timePicker;
	private ViewSwitcher	viewSwitcher;
	private Calendar		mCalendar;

	//----------------------------------------------------------
	public DateTimePicker(Context context) 
	{
		this(context, null);
	}

	//----------------------------------------------------------
	public DateTimePicker(Context context, AttributeSet attrs) 
	{
		this(context, attrs, 0);
	}

	//----------------------------------------------------------
	public DateTimePicker(Context context, AttributeSet attrs, int defStyle) 
	{
		super(context, attrs, defStyle);

		// Get LayoutInflater instance
		final LayoutInflater inflater = 
				(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		// Inflate myself
		inflater.inflate(R.layout.datetimepicker, this, true);

		// Inflate the date and time picker views
		final LinearLayout datePickerView = 
				(LinearLayout) inflater.inflate(R.layout.datepicker, null);
		final LinearLayout timePickerView = 
				(LinearLayout) inflater.inflate(R.layout.timepicker, null);

		// Grab a Calendar instance
		mCalendar = Calendar.getInstance();
		// Grab the ViewSwitcher so we can attach our picker views to it
		viewSwitcher = (ViewSwitcher) this.findViewById(R.id.DateTimePickerVS);

		// Init date picker
		datePicker = (DatePicker) datePickerView.findViewById(R.id.DatePicker);
		datePicker.init(mCalendar.get(Calendar.YEAR), mCalendar.get(Calendar.MONTH), 
				mCalendar.get(Calendar.DAY_OF_MONTH), this);

		// Init time picker
		timePicker = (TimePicker) timePickerView.findViewById(R.id.TimePicker);
		timePicker.setOnTimeChangedListener(this);
		
		// Init day view
		dayView = (TextView) datePickerView.findViewById(R.id.DayView);
		dayView2 = (TextView) timePickerView.findViewById(R.id.DayView2);
		// Set the current date to date view
		DateFormat dateFormat = new SimpleDateFormat("EEE, hh:mm aaa", Locale.US);
		String timeInfo = dateFormat.format(mCalendar.getTime());
		dateFormat = new SimpleDateFormat("EEE, MM-dd-yyyy", Locale.US);
		String dateInfo = dateFormat.format(mCalendar.getTime());
		dayView.setText(timeInfo);
		dayView2.setText(dateInfo);

		// Handle button clicks
		((Button) findViewById(R.id.SwitchToTime)).setOnClickListener(this); // shows the time picker
		((Button) findViewById(R.id.SwitchToDate)).setOnClickListener(this); // shows the date picker

		// Populate ViewSwitcher
		viewSwitcher.addView(datePickerView, 0);
		viewSwitcher.addView(timePickerView, 1);
	}

	// Called every time the user changes DatePicker values
	//----------------------------------------------------------
	public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) 
	{
		// Update the internal Calendar instance
		mCalendar.set(year, monthOfYear, dayOfMonth, mCalendar.get(Calendar.HOUR_OF_DAY), mCalendar.get(Calendar.MINUTE));
		
		// Set the day views as well
		DateFormat dateFormat = new SimpleDateFormat("EEE, hh:mm aaa", Locale.US);
		String timeInfo = dateFormat.format(mCalendar.getTime());
		dateFormat = new SimpleDateFormat("EEE, MMM d, yyyy", Locale.US);
		String dateInfo = dateFormat.format(mCalendar.getTime());
		dayView.setText(timeInfo);
		dayView2.setText(dateInfo);
	}

	// Called every time the user changes TimePicker values
	//----------------------------------------------------------
	public void onTimeChanged(TimePicker view, int hourOfDay, int minute) 
	{
		// Update the internal Calendar instance
		mCalendar.set(mCalendar.get(Calendar.YEAR), mCalendar.get(Calendar.MONTH), 
				mCalendar.get(Calendar.DAY_OF_MONTH), hourOfDay, minute);
		
		// Set the day views as well
		DateFormat dateFormat = new SimpleDateFormat("EEE, hh:mm aaa", Locale.US);
		String timeInfo = dateFormat.format(mCalendar.getTime());
		dayView.setText(timeInfo);
	}

	// Handle button clicks
	//----------------------------------------------------------
	public void onClick(View v) 
	{
		switch (v.getId()) 
		{
			case R.id.SwitchToDate:
				v.setEnabled(false);
				findViewById(R.id.SwitchToTime).setEnabled(true);
				viewSwitcher.showPrevious();
				break;

			case R.id.SwitchToTime:
				v.setEnabled(false);
				findViewById(R.id.SwitchToDate).setEnabled(true);
				viewSwitcher.showNext();
				break;
		}
	}

	// Convenience wrapper for internal Calendar instance
	//----------------------------------------------------------
	public int get(final int field) 
	{
		return mCalendar.get(field);
	}

	// Reset DatePicker, TimePicker and internal Calendar instance
	//----------------------------------------------------------
	public void reset() 
	{
		final Calendar c = Calendar.getInstance();
		updateDate(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH));
		updateTime(c.get(Calendar.HOUR_OF_DAY),c.get(Calendar.MINUTE));
	}

	// Convenience wrapper for internal Calendar instance
	//----------------------------------------------------------
	public long getDateTimeMillis() 
	{
		return mCalendar.getTimeInMillis();
	}

	// Convenience wrapper for internal TimePicker instance
	//----------------------------------------------------------
	public void setIs24HourView(boolean is24HourView) 
	{
		timePicker.setIs24HourView(is24HourView);
	}
	
	// Convenience wrapper for internal TimePicker instance
	//----------------------------------------------------------
	public boolean is24HourView() 
	{
		return timePicker.is24HourView();
	}

	// Convenience wrapper for internal DatePicker instance
	//----------------------------------------------------------
	public void updateDate(int year, int monthOfYear, int dayOfMonth) 
	{
		datePicker.updateDate(year, monthOfYear, dayOfMonth);
	}

	// Convenience wrapper for internal TimePicker instance
	//----------------------------------------------------------
	public void updateTime(int currentHour, int currentMinute) 
	{
		timePicker.setCurrentHour(currentHour);
		timePicker.setCurrentMinute(currentMinute);
	}
	
	//----------------------------------------------------------
	public String getTime()
	{
		DateFormat timeFormat = new SimpleDateFormat("HHmm");
		return timeFormat.format(mCalendar.getTime());
	}
	
	//----------------------------------------------------------
	public String getDate()
	{
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		return dateFormat.format(mCalendar.getTime());
	}
	
	//----------------------------------------------------------
	public String getDateTime()
	{
		DateFormat dateFormat = 
				new SimpleDateFormat("EEE, MMM d, ''yy 'at' hh:mm aaa");
		return dateFormat.format(mCalendar.getTime());
	}
	
	//----------------------------------------------------------
	public Calendar getCalendar()
	{
		return mCalendar;
	}
}